import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
// 封装一个函数用来控制局部权限
function hasUrl(url) {
  return store.getters.getUserInfo.menus_url.some(item => item == url)
}
Vue.use(Router)
export const indexRotes = [{
    path: '/menu',
    component: () => import('@/views/meun/menu.vue'),
    name: '菜单管理',
    beforeEnter(to, from, next) {
      hasUrl('/menu') ? next() : next('/home')
    }
  },
  {
    path: '/roler',
    component: () => import('@/views/roler/roler.vue'),
    name: '角色管理',
    beforeEnter(to, from, next) {
      hasUrl('/roler') ? next() : next('/home')
    }
  },
  {
    path: '/manger',
    component: () => import('@/views/manger/manger.vue'),
    name: '管理员管理',
    beforeEnter(to, from, next) {
      hasUrl('/manger') ? next() : next('/home')
    }
  },
  {
    path: '/goodscate',
    component: () => import('@/views/goodscate/goodscate.vue'),
    name: '商品分类',
    beforeEnter(to, from, next) {
      hasUrl('/goodscate') ? next() : next('/home')
    }
  },
  {
    path: '/goodsspecs',
    component: () => import('@/views/goodsspecs/goodsspecs.vue'),
    name: '商品规格',
    beforeEnter(to, from, next) {
      hasUrl('/goodsspecs') ? next() : next('/home')
    }
  },
  {
    path: '/goods',
    component: () => import('@/views/goods/goods.vue'),
    name: '商品管理',
    beforeEnter(to, from, next) {
      hasUrl('/goods') ? next() : next('/home')
    }
  },
  {
    path: '/banner',
    component: () => import('@/views/banner/banner.vue'),
    name: '轮播图管理',
    beforeEnter(to, from, next) {
      hasUrl('/banner') ? next() : next('/home')
    }
  },
  {
    path: '/member',
    component: () => import('@/views/member/member.vue'),
    name: '会员管理',
    beforeEnter(to, from, next) {
      hasUrl('/member') ? next() : next('/home')
    }
  },
  {
    path: '/seck',
    component: () => import('@/views/seck/seck.vue'),
    name: '秒杀管理',
    beforeEnter(to, from, next) {
      hasUrl('/seck') ? next() : next('/home')
    }
  }
]
let router = new Router({
  routes: [{
      path: '/login',
      component: () => import('@/pages/login.vue')
    },
    {
      path: '/index',
      component: () => import('@/pages/index.vue'),
      children: [{
          path: '/home',
          component: () => import('@/views/home.vue')
        },
        ...indexRotes,
        {
          path: '',
          redirect: '/home'
        }
      ]
    },
    {
      path: '*',
      redirect: '/login'
    }
  ]
})

//全局导航守卫之登录拦截
router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    next()
    return
  }
  if (store.getters.getUserInfo) {
    next()
    return
  }
  next('/login')
})

export default router
